package com.bw.shop2208a.search

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.bw.shop2208a.entity.DataGoodsEntity
import com.bw.shop2208a.entity.GoodsEntity
import com.bw.shop2208a.mvvm.BaseViewModel
import com.bw.shop2208a.utils.Apiservice
import kotlinx.coroutines.launch

class SearchViewModel : BaseViewModel() {
    //定义搜索成功的返回值--接受的时一个list集合  (不包含code,message)
    var successSearch = MutableLiveData<List<DataGoodsEntity>>()
    var failSearch = MutableLiveData<String>()

    fun search(name: String) {
        viewModelScope.launch {
            try {
                val result = apiservice.getGoods(0, 1, 100)
                val data = result.data.filter { it.goods_desc.contains(name) }
                successSearch.value = data
            } catch (e: Exception) {
                failSearch.value = e.message
            }
        }
    }
}